package com.cloudkinto.controller.platform;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.dto.platform.PlatformOrderDetailRes;
import com.cloudkinto.dto.platform.order.PlatOrderPageRes;
import com.cloudkinto.service.channel.LogisticsChannelService;
import com.cloudkinto.service.consumer.vo.qoo10.Qoo10ConsumerReq;
import com.cloudkinto.service.consumer.vo.shopify.WowmaConsumerReq;
import com.cloudkinto.service.mail.SendMailService;
import com.cloudkinto.service.mail.vo.SendMailReq;
import com.cloudkinto.service.platform.*;
import com.cloudkinto.service.platform.qoo10.PlatformOrderQoo10Service;
import com.cloudkinto.service.platform.qoo10.vo.PlatformOrderUpdateReq;
import com.cloudkinto.service.platform.qoo10.vo.Qoo10ApiRes;
import com.cloudkinto.service.platform.vo.PlatformOrderSetPushStoreReq;
import com.cloudkinto.service.platform.vo.PlatformOrderSyncReq;
import com.cloudkinto.service.platform.vo.order.MakeShipReq;
import com.cloudkinto.service.platform.vo.order.UpdateToFinishErrorRes;
import com.cloudkinto.service.platform.vo.rakuten.GetOrderByTimeReq;
import com.cloudkinto.service.platform.vo.rakuten.RakutenApiRes;
import com.cloudkinto.service.platform.vo.rakuten.RakutenSyncReq;
import com.cloudkinto.vo.channel.lc.LcOptionRes;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.cloudkinto.base.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 平台订单 前端控制器
 * </p>
 * @author hua
 * @since 2024-12-25
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "平台订单模块")
@RequestMapping("/api/admin/oms/PlatformOrder")
public class PlatformOrderOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(PlatformOrderOmsController.class);

    private final PlatformOrderService service;
    private final LogisticsChannelService logisticsChannelService;
    private final PlatformOrderShopifySyncService platformOrderShopifySyncService;
    private final PlatformOrderRakutenSyncService platformOrderRakutenSyncService;
    private final PlatformOrderAmazonSyncService platformOrderAmazonSyncService;
    private final PlatformOrderYahooSyncService platformOrderYahooSyncService;
    private final PlatformOrderMakeshopSyncService platformOrderMakeshopSyncService;
    private final PlatformOrderWowmaSyncService platformOrderWowmaSyncService;
    private final PlatformOrderQoo10Service platformOrderQoo10Service;
    private final PlatformOrderExportService platformOrderExportService;
    private final SendMailService sendMailService;


    @UserSessionRequired
    @GetMapping("/getPlatformOrderTrackTemplate")
    @ApiOperation(value = "获取导入快递单号模板", notes = "获取导入快递单号模板")
    public void getPlatformOrderTrackTemplate(HttpServletResponse response) throws Exception {
        service.getPlatformOrderTrackTemplate(response, getTenantId());
    }

    @UserSessionRequired
    @PostMapping("/importTrackNo")
    @ApiOperation(value = "导入快递单号", notes = "导入快递单号")
    public SingleResult importNormalInbound(MultipartFile file) {
        service.importTrackNo(file, getCurrentUserId(), getCompanyId(), getTenantId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @PutMapping("/setPushStoreList")
    @ApiOperation(value = "设置自动审核店铺", notes = "设置自动审核店铺")
    public SingleResult setPushStoreList(@RequestBody @Validated PlatformOrderSetPushStoreReq req) {
        service.setPushStoreList(req, getCompanyId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping("/resetRedisSyncTime")
    @ApiOperation(value = "重置订单同步时间", notes = "重置订单同步时间")
    public SingleResult resetRedisSyncTime() {
        service.resetRedisSyncTime(true);
        return SingleResult.success();
    }


    @UserSessionRequired
    @PostMapping("/updateRakutenOrders")
    @ApiOperation(value = "手动同步乐天订单", notes = "手动同步乐天订单")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = LcOptionRes.class))
    public SingleResult updateRakutenOrders(@RequestBody @Validated PlatformOrderSyncReq req) {
        platformOrderRakutenSyncService.manualUpdateRakutenOrders(req.getStoreIds(), null, null, getCompanyId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @GetMapping("/getLcForChoice")
    @ApiImplicitParams({@ApiImplicitParam(name = "warehouseId", value = "仓库id")})
    @ApiOperation(value = "根据仓库id 获取物流渠道", notes = "根据仓库id 获取物流渠道")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = LcOptionRes.class))
    public SingleResult getLcForChoice(Long warehouseId) {
        return SingleResult.success("查询成功",logisticsChannelService.getLcForChoice(warehouseId, getCompanyId(), getTenantId()));
    }


    @UserSessionRequired
    @PutMapping("/updateToDeal")
    @ApiOperation(value = "转为待处理(已取消订单)", notes = "转为待处理(已取消订单)")
    public SingleResult updateToDeal(@RequestBody @Validated MakeShipReq req) {
        service.updateToDeal(req, getCurrentUserId(), getCompanyId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @PutMapping("/updateToFinish")
    @ApiOperation(value = "标记为已发货(已发货前三个状态)，订单将会不可以标发", notes = "标记为已发货(已发货前三个状态)，订单将会不可以标发")
    public SingleResult updateToFinish(@RequestBody @Validated MakeShipReq req) {
        List<UpdateToFinishErrorRes> errorResList = service.updateToFinish(req, getCurrentUserId(), getCompanyId(), getTenantId());
        return SingleResult.success(errorResList);
    }

    @UserSessionRequired
    @PutMapping("/updateToCancel")
    @ApiOperation(value = "取消", notes = "取消")
    public SingleResult updateToCancel(@RequestBody @Validated MakeShipReq req) {
        service.updateToCancel(req, getCurrentUserId(), getCompanyId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "平台订单根据id删除", notes = "平台订单根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id,userId));
    }



    @UserSessionRequired
    @PutMapping("/updatePlatformOrder")
    @ApiOperation(value = "更新平台订单", notes = "更新平台订单")
    public SingleResult updatePlatformOrder(@RequestBody @Validated PlatformOrderUpdateReq req) {
        service.updatePlatformOrder(req, getCurrentUserId(), getCompanyId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "平台订单根据id查看详情", notes = "平台订单根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatformOrderDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id, getCompanyId()));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "平台订单查询初始化", notes = "平台订单查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCompanyId(), getTenantId());
    }


    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "平台订单查询", notes = "平台订单查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatOrderPageRes.class))
    public SingleResult list(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, Object> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "平台订单查询（分页）", notes = "平台订单查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time(订单同步时间)， audit_time(审核时间)， ship_time(发货时间), cancel_time(取消时间)  等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "deliveryOptionType", value = "履约方式"),
            @ApiImplicitParam(name = "markShipmentStatus", value = "标发状态"),
            @ApiImplicitParam(name = "payStatus", value = "0 未付款 1已付款"),
            @ApiImplicitParam(name = "channelId", value = "物流渠道"),
            @ApiImplicitParam(name = "storeId", value = "店铺"),
            @ApiImplicitParam(name = "whId", value = "仓库"),
            @ApiImplicitParam(name = "site", value = "站点"),
            @ApiImplicitParam(name = "reserveFlag", value = "单据类型 0 普通单 1预约单"),
            @ApiImplicitParam(name = "fulfillmentChannel", value = "配送类型 AFN MFN"),
            @ApiImplicitParam(name = "platformCode", value = "平台"),
            @ApiImplicitParam(name = "auditCode", value = "审核失败Code"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "platformSku", value = "平台sku"),
            @ApiImplicitParam(name = "sku", value = "系统sku"),
            @ApiImplicitParam(name = "postcode", value = "邮编"),
            @ApiImplicitParam(name = "receiver", value = "收件人"),
            @ApiImplicitParam(name = "telephone", value = "电话"),
            @ApiImplicitParam(name = "buyerName", value = "买家姓名"),
            @ApiImplicitParam(name = "platformOrderNo", value = "平台单号"),
            @ApiImplicitParam(name = "orderNo", value = "订单号"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatOrderPageRes.class))
    public PageResult<PlatOrderPageRes> listPage(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    @UserSessionRequired
    @GetMapping("/listPageSendStatus")
    @ApiOperation(value = "平台订单邮件发送状态查询（分页）", notes = "平台订单邮件发送状态查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "pageSize", value = "页数"),
            @ApiImplicitParam(name = "current", value = "当前页"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = ""))
    public PageResult listPageSendStatus(HttpServletRequest request){
        Map map = new HashMap<>();
        handleMap(request, map);
        return service.getListPageMailSendStatus(map);
    }

    private  void handleMap(HttpServletRequest request, Map map) {
        String status = request.getParameter("status");
        map.put("status", status);
        String sendMethod= request.getParameter("sendMethod");
        map.put("sendMethod", sendMethod);
        /*   String sendTime = request.getParameter("sendTime");*/
        String platformOrderNo = request.getParameter("platformOrderNo");
        map.put("platformOrderNo", platformOrderNo);
        String platformCode = request.getParameter("platformCode");
        map.put("platformCode", platformCode);
        String receiver = request.getParameter("receiver");
        map.put("receiver", receiver);
        String shop = request.getParameter("shop");
        map.put("shop", shop);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String templateName = request.getParameter("templateName");
        map.put("templateName", templateName);
        map.put("current", current);
        map.put("pageSize", size);
    }


    @UserSessionRequired
    @PostMapping("/previewContent")
    @ApiOperation(value = "预览邮件内容")
    public SingleResult preview(@RequestBody SendMailReq req) throws Exception {
        return sendMailService.previewContent(req);
    }

    public Map<String, Object> getMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String ids = request.getParameter("ids");
        if (StringUtils.isNotBlank(ids)) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        String payStatus = request.getParameter("payStatus");
        if (StringUtils.isNotBlank(payStatus)) {
            map.put("payStatus", payStatus);
        }
        String reserveFlag = request.getParameter("reserveFlag");
        String fulfillmentChannel = request.getParameter("fulfillmentChannel");
        map.put("reserveFlag", reserveFlag);
        map.put("fulfillmentChannel", fulfillmentChannel);
        String auditCode = request.getParameter("auditCode");
        map.put("auditCode", auditCode);
        String dateColumn = request.getParameter("dateColumn");
        map.put("dateColumn", dateColumn);
        String beginTime = request.getParameter("beginTime");
        map.put("beginTime", beginTime);
        String endTime = request.getParameter("endTime");
        map.put("endTime", endTime);

        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        String orderNo = request.getParameter("orderNo");
        map.put("orderNo", orderNo);
        String platformOrderNo = request.getParameter("platformOrderNo");
        if (StringUtils.isNotBlank(platformOrderNo)) {
            map.put("platformOrderNo", platformOrderNo.trim());
        }
        String buyerName = request.getParameter("buyerName");
        map.put("buyerName", buyerName);
        String receiver = request.getParameter("receiver");
        map.put("receiver", receiver);
        String postcode = request.getParameter("postcode");
        map.put("postcode", postcode);
        String telephone = request.getParameter("telephone");
        if (StringUtils.isNotBlank(telephone)) {
            map.put("telephone", telephone.trim());
        }
        String platformSku = request.getParameter("platformSku");
        map.put("platformSku", platformSku);
        String sku = request.getParameter("sku");
        map.put("sku", sku);
        String deliveryOptionType = request.getParameter("deliveryOptionType");
        map.put("deliveryOptionType", deliveryOptionType);
        String markShipmentStatus = request.getParameter("markShipmentStatus");
        if (StringUtils.isNotBlank(markShipmentStatus)) {
            String[] split = markShipmentStatus.split(",");
            map.put("markShipmentStatus", Arrays.asList(split));
        }
        String channelId = request.getParameter("channelId");
        if (StringUtils.isNotBlank(channelId)) {
            String[] split = channelId.split(",");
            map.put("channelId", Arrays.asList(split));
        }
        String cancelWay = request.getParameter("cancelWay");
        if (StringUtils.isNotBlank(cancelWay)) {
            String[] split = cancelWay.split(",");
            map.put("cancelWay", Arrays.asList(split));
        }

        String whId = request.getParameter("whId");
        if (StringUtils.isNotBlank(whId)) {
            String[] split = whId.split(",");
            map.put("whId", Arrays.asList(split));
        }
        String site = request.getParameter("site");
        String storeId = request.getParameter("storeId");
        map.put("site", site);
        if (StringUtils.isNotBlank(storeId)) {
            String[] split = storeId.split(",");
            map.put("storeId", Arrays.asList(split));
        }
        String status = request.getParameter("status");
        String platformCode = request.getParameter("platformCode");
        map.put("status", status);

        if (StringUtils.isNotBlank(platformCode)) {
            String[] split = platformCode.split(",");
            map.put("platformCode", Arrays.asList(split));
        }
        String countryRegionCode = request.getParameter("countryRegionCode");
        if (StringUtils.isNotBlank(countryRegionCode)) {
            String[] split = countryRegionCode.split(",");
            map.put("countryRegionCode", Arrays.asList(split));
        }
        String sort = request.getParameter("sort");
        String sortColumn = request.getParameter("sortColumn");
        map.put("sort", sort);
        map.put("sortColumn", sortColumn);
        return map;
    }



    @UserSessionRequired
    @GetMapping("/statusStatistics")
    @ApiOperation(value = "状态统计", notes = "状态统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time(订单同步时间)， audit_time(审核时间)， ship_time(发货时间), cancel_time(取消时间)  等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "deliveryOptionType", value = "履约方式"),
            @ApiImplicitParam(name = "markShipmentStatus", value = "标发状态"),
            @ApiImplicitParam(name = "channelId", value = "物流渠道"),
            @ApiImplicitParam(name = "storeId", value = "店铺"),
            @ApiImplicitParam(name = "whId", value = "仓库"),
            @ApiImplicitParam(name = "site", value = "站点"),
            @ApiImplicitParam(name = "platformCode", value = "平台"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "platformSku", value = "平台sku"),
            @ApiImplicitParam(name = "sku", value = "系统sku"),
            @ApiImplicitParam(name = "postcode", value = "邮编"),
            @ApiImplicitParam(name = "receiver", value = "收件人"),
            @ApiImplicitParam(name = "buyerName", value = "买家姓名"),
            @ApiImplicitParam(name = "platformOrderNo", value = "平台单号"),
            @ApiImplicitParam(name = "orderNo", value = "订单号"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatOrderPageRes.class))
    public SingleResult statusStatistics(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        return SingleResult.success(service.statusStatistics(map));
    }


    @UserSessionRequired
    @GetMapping("/check")
    @ApiOperation(value = "审核", notes = "审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "勾选的数据 多个 逗号隔开"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time(订单同步时间)， audit_time(审核时间)， ship_time(发货时间), cancel_time(取消时间)  等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "deliveryOptionType", value = "履约方式"),
            @ApiImplicitParam(name = "markShipmentStatus", value = "标发状态"),
            @ApiImplicitParam(name = "channelId", value = "物流渠道"),
            @ApiImplicitParam(name = "storeId", value = "店铺"),
            @ApiImplicitParam(name = "whId", value = "仓库"),
            @ApiImplicitParam(name = "site", value = "站点"),
            @ApiImplicitParam(name = "platformCode", value = "平台"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "platformSku", value = "平台sku"),
            @ApiImplicitParam(name = "sku", value = "系统sku"),
            @ApiImplicitParam(name = "postcode", value = "邮编"),
            @ApiImplicitParam(name = "receiver", value = "收件人"),
            @ApiImplicitParam(name = "buyerName", value = "买家姓名"),
            @ApiImplicitParam(name = "platformOrderNo", value = "平台单号"),
            @ApiImplicitParam(name = "orderNo", value = "订单号"),
    })
    public SingleResult check(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        service.check(map, getCurrentUserId(), getCompanyId(), getTenantId());
        return SingleResult.success();
    }



    @UserSessionRequired
    @GetMapping("/distributeWh")
    @ApiOperation(value = "分配仓库&物流渠道", notes = "分配仓库&物流渠道")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "distributeWhId", value = "分配的仓库"),
            @ApiImplicitParam(name = "distributeChannelId", value = "分配的物流渠道"),
            @ApiImplicitParam(name = "ids", value = "勾选的数据 多个 逗号隔开"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time(订单同步时间)， audit_time(审核时间)， ship_time(发货时间), cancel_time(取消时间)  等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "deliveryOptionType", value = "履约方式"),
            @ApiImplicitParam(name = "markShipmentStatus", value = "标发状态"),
            @ApiImplicitParam(name = "channelId", value = "物流渠道"),
            @ApiImplicitParam(name = "storeId", value = "店铺"),
            @ApiImplicitParam(name = "whId", value = "仓库"),
            @ApiImplicitParam(name = "site", value = "站点"),
            @ApiImplicitParam(name = "platformCode", value = "平台"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "platformSku", value = "平台sku"),
            @ApiImplicitParam(name = "sku", value = "系统sku"),
            @ApiImplicitParam(name = "postcode", value = "邮编"),
            @ApiImplicitParam(name = "receiver", value = "收件人"),
            @ApiImplicitParam(name = "buyerName", value = "买家姓名"),
            @ApiImplicitParam(name = "platformOrderNo", value = "平台单号"),
            @ApiImplicitParam(name = "orderNo", value = "订单号"),
    })
    public SingleResult distributeWh(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        String distributeWhId = request.getParameter("distributeWhId");
        String distributeChannelId = request.getParameter("distributeChannelId");
        map.put("distributeWhId", distributeWhId);
        map.put("distributeChannelId", distributeChannelId);

        service.distributeWh(map, getCurrentUserId(), getCompanyId(), getTenantId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @GetMapping("/ableAutoASubmit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "autoSubmit", value = "1 选中 0未选中"),
    })
    @ApiOperation(value = "修改审核成功自动提交仓库", notes = "审核成功自动提交仓库")
    public SingleResult ableAutoASubmit(Integer autoSubmit) {
        return SingleResult.success(service.ableAutoASubmit(autoSubmit, getCompanyId()));
    }
    @UserSessionRequired
    @GetMapping("/markShip")
    @ApiOperation(value = "标发", notes = "标发")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "勾选的数据 多个 逗号隔开"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time(订单同步时间)， audit_time(审核时间)， ship_time(发货时间), cancel_time(取消时间)  等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "deliveryOptionType", value = "履约方式"),
            @ApiImplicitParam(name = "markShipmentStatus", value = "标发状态"),
            @ApiImplicitParam(name = "channelId", value = "物流渠道"),
            @ApiImplicitParam(name = "storeId", value = "店铺"),
            @ApiImplicitParam(name = "whId", value = "仓库"),
            @ApiImplicitParam(name = "site", value = "站点"),
            @ApiImplicitParam(name = "platformCode", value = "平台"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "platformSku", value = "平台sku"),
            @ApiImplicitParam(name = "sku", value = "系统sku"),
            @ApiImplicitParam(name = "postcode", value = "邮编"),
            @ApiImplicitParam(name = "receiver", value = "收件人"),
            @ApiImplicitParam(name = "buyerName", value = "买家姓名"),
            @ApiImplicitParam(name = "platformOrderNo", value = "平台单号"),
            @ApiImplicitParam(name = "orderNo", value = "订单号"),
    })
    public SingleResult markShip(HttpServletRequest request) {
        Map<String, Object> map = this.getMap(request);
        service.markShip(map, "manual", getCurrentUserId(), getCompanyId(), getTenantId());
        return SingleResult.success();
    }

//    @UserSessionRequired
//    @PutMapping("/makeShipQoo10")
//    @ApiOperation(value = "Qoo10订单标发", notes = "Qoo10订单标发")
//    public SingleResult update(@RequestBody @Validated Qoo10MarkShipConsumerReq req) {
//        Long userId = getCurrentUserId();
//            platformOrderQoo10Service.makeShip(req,userId);
//        return SingleResult.success();
//    }



        @UserSessionRequired
    @GetMapping("/getFulfillmentOrders")
    @ApiOperation(value = "Shopify订单标发", notes = "Qoo10订单标发")
    public SingleResult getFulfillmentOrders(Long storeId, String orderId) {
        Long userId = getCurrentUserId();
        platformOrderShopifySyncService.getFulfillmentOrders(storeId, orderId);
        return SingleResult.success();
    }


    @UserSessionRequired
    @GetMapping("/platformOrderExport")
    @ApiOperation(value = "导出平台订单列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "勾选数据id", dataType = "arr"),
    })
    public void platformOrderExport(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getMap(request);
        service.export(map, response);
    }

    @UserSessionRequired
    @GetMapping("/platformOrderExportNew")
    @ApiOperation(value = "导出平台订单列表新版", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "勾选数据id", dataType = "arr"),
    })
    public SingleResult platformOrderExportNew(HttpServletRequest request) {
        Map<String, Object> map = this.getMap(request);
        map.put("createByName", getUserName());
        Long taskId = platformOrderExportService.requestExport(map);
        return SingleResult.success("正在导出，请稍后再下载列表中查看", taskId);
    }

    @UserSessionRequired
    @GetMapping("/downloadExportFile")
    @ApiOperation(value = "下载导出数据", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id"),
    })
    public void downloadExportFile(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        platformOrderExportService.downloadExportFile(id, response);
    }

//    @UserSessionRequired
    @GetMapping("/testGetRakutenOrderByNumber")
    @ApiOperation(value = "乐天根据订单号获取订单", notes = "乐天根据订单号获取订单")
    public SingleResult testGetRakutenOrderByNumber(String orderNumber, Long storeId) {
        RakutenApiRes apiRes = platformOrderRakutenSyncService.testGetOrderByNumber(orderNumber, storeId);
        return SingleResult.success(apiRes);
    }

    @UserSessionRequired
    @PostMapping("/testGetOrderByTime")
    @ApiOperation(value = "乐天根据时间同步订单", notes = "乐天根据时间同步订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间 yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "endDate", value = "结束时间 yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "storeId", value = "店铺"),
    })
    public SingleResult testGetOrderByTime(@RequestBody @Validated GetOrderByTimeReq req) {
        platformOrderRakutenSyncService.testGetOrderByTime(req.getStartDate(), req.getEndDate(), req.getOrderNumbers(), req.getDateType(), req.getStoreId(), getCurrentUserId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @GetMapping("/testGetAmazonOrderByNumber")
    @ApiOperation(value = "亚马逊根据订单号同步订单", notes = "亚马逊根据订单号同步订单")
    public SingleResult testGetAmazonOrderByNumber(String orderNumber, String beginTime, String endTime, Long storeId) {
        String apiRes = platformOrderAmazonSyncService.testGetOrderByNumber(orderNumber,
                StringUtils.isBlank(beginTime) ? null: DateUtils.stringFormatToDate(beginTime, "yyyy-MM-dd HH:mm:ss"),
                StringUtils.isBlank(endTime) ? null: DateUtils.stringFormatToDate(endTime, "yyyy-MM-dd HH:mm:ss"),
                storeId);
        return SingleResult.success(apiRes);
    }

    @UserSessionRequired
    @GetMapping("/getOrderByNumber")
    @ApiOperation(value = "亚马逊根据订单号获取订单", notes = "亚马逊根据订单号获取订单")
    public SingleResult getOrderByNumber(String orderNumber, Long storeId) {
        Map<String, Object> orderByNumber = platformOrderAmazonSyncService.getOrderByNumber(orderNumber, storeId);
        return SingleResult.success(orderByNumber);
    }


    @UserSessionRequired
    @GetMapping("/testGetYahooOrderByNumber")
    @ApiOperation(value = "Yahoo根据订单号获取订单", notes = "Yahoo根据订单号获取订单")
    public SingleResult testGetYahooOrderByNumber(String orderNumber, Long storeId) {
        Object result = platformOrderYahooSyncService.testGetOrderByNumber(orderNumber, storeId);
        return SingleResult.success(result);
    }

    @UserSessionRequired
    @GetMapping("/testGetWowmaOrderByNumber")
    @ApiOperation(value = "Wowma根据订单号获取订单", notes = "Wowma根据订单号获取订单")
    public SingleResult testGetWowmaOrderByNumber(String orderNumber, Long storeId) {
        Object result = platformOrderWowmaSyncService.testGetOrderByNumber(orderNumber, storeId);
        return SingleResult.success(result);
    }

    @UserSessionRequired
    @GetMapping("/testGetWowmaOrderByTime")
    @ApiOperation(value = "Wowma根据时间同步订单", notes = "Wowma根据时间同步订单")
    public SingleResult testGetWowmaOrderByTime(String beginTime, String endTime, Long storeId) {
        WowmaConsumerReq req = new WowmaConsumerReq(storeId, DateUtils.stringFormatToDate(beginTime, "yyyy-MM-dd HH:mm:ss"), DateUtils.stringFormatToDate(endTime, "yyyy-MM-dd HH:mm:ss"));
        Object result = platformOrderWowmaSyncService.updatWowmaOrders(req);
        return SingleResult.success(result);
    }


    @GetMapping("/testGetQoo10OrderByNumber")
    @ApiOperation(value = "qoo10根据订单号获取订单", notes = "qoo10根据订单号获取订单")
    public SingleResult testGetQoo10OrderByNumber(String orderNumber, Long storeId) {
        Qoo10ApiRes apiRes = platformOrderQoo10Service.getOrderByOrderNo(orderNumber, storeId);
        return SingleResult.success(apiRes);
    }

    @GetMapping("/testGetQoo10OrderByTime")
    @ApiOperation(value = "qoo10根据时间同步订单", notes = "qoo10根据时间同步订单")
    public SingleResult testGetQoo10OrderByTime(String orderNumber, String beginTime, String endTime, Long storeId) {
        String apiRes = platformOrderQoo10Service.updateQoo10Orders(new Qoo10ConsumerReq(storeId,  StringUtils.isBlank(beginTime) ? null: DateUtils.stringFormatToDate(beginTime, "yyyy-MM-dd HH:mm:ss"),
                StringUtils.isBlank(endTime) ? null: DateUtils.stringFormatToDate(endTime, "yyyy-MM-dd HH:mm:ss"), "1",
                3, orderNumber, null
                ));
        return SingleResult.success(apiRes);
    }


//    @UserSessionRequired
    @GetMapping("/testGetYahooOrderByTime")
    @ApiOperation(value = "yahoo根据时间同步订单", notes = "yahoo根据时间同步订单")
    public SingleResult testGetYahooOrderByTime(String orderNumber, String beginTime, String endTime, Long storeId) {
        String apiRes = platformOrderYahooSyncService.syncOrderManual(orderNumber,
                StringUtils.isBlank(beginTime) ? null: DateUtils.stringFormatToDate(beginTime, "yyyy-MM-dd HH:mm:ss"),
                StringUtils.isBlank(endTime) ? null: DateUtils.stringFormatToDate(endTime, "yyyy-MM-dd HH:mm:ss"),
                storeId);
        return SingleResult.success(apiRes);
    }



//    @UserSessionRequired
//    @GetMapping("/testUpdateInventoryAmazon")
//    @ApiOperation(value = "测试库存回传", notes = "测试库存回传")
//    public SingleResult testUpdateInventoryAmazon(Long storeId, String sku, Integer quantity) {
//        platformOrderAmazonSyncService.testInventory(storeId, sku, quantity);
//        return SingleResult.success();
//    }
//
//
//    @UserSessionRequired


    @UserSessionRequired
    @PostMapping("/cancelMatchSysProduct")
    @ApiOperation(value = "取消SKU匹配", notes = "")
    public SingleResult cancelMatchSysProduct(@RequestBody @Validated List<Long> ids) {
        service.cancelMatchSysProduct(ids, getCurrentUserId(), getCompanyId());
        return SingleResult.success();
    }
}

